<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
 * Handles the stack of node objects.  Contains the ability
 * to push, pop, and return the last item of the stack.  Requires
 * all nodes that are pushed onto the stack to be an object.
 */
class NodeStack_Core {
	// The array that is the stack
	protected $stack = array();
	
	// Keeps track of what the current key of
	// the last element on the node stack is.
	// This starts at -1 because when we push
	// the first element on, it will become 0.
	protected $key = -1;
	
	/**
	 * Pushes an element on to the node stack.
	 * 
	 * @param  object  Node object (Closingnode_Core $node)
	 */
	final public function push(Closingnode_Core $node)
	{
		$this->stack[] = $node;
		$this->key++;
	}
	
	/**
	 * Pop the last element off of the node stack.
	 * 
	 * @return  object  Node object
	 */
	final public function pop()
	{
		$this->key--;
		return array_pop($this->stack);
	}
	
	/**
	 * Return the last element of the node stack
	 * 
	 * @return  object  Node object
	 */
	final public function last()
	{
		return $this->stack[$this->key];
	}
}
